//
//  GotoAccountViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/20.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "GotoAccountViewController.h"
#import "PayViewController.h"
#import "UseDiscountViewController.h"
@interface GotoAccountViewController ()

@property (nonatomic, copy) NSString *orderString;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
//@property (nonatomic, copy) NSString *num;
@property (nonatomic, strong) UILabel *reminderLabel;
@property (nonatomic, assign) int i;
@property (nonatomic, copy) NSString *ticketId;//
@property (nonatomic, copy) NSString *titleName;//是否有优惠券

@end

@implementation GotoAccountViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提交订单";
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, -64, GWIDTH, 64)];
    vi.backgroundColor = ColorWhite;
    [self.view addSubview:vi];
    
    [self addBackItem];
    _i = 1;
    [self initView];
    [self startNetwork];
    
//    [self network];
}


- (void)startNetwork
{
    //生成订单
    NSDictionary *param = @{@"goodsId"  : STRING(self.goodId),//测试数据
                            @"ticketId" : STRING(self.ticketId),
                            @"sum"      : STRING(self.numberLabel.text)
                            };
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/corder?goodsId=%@&ticketId=%@&sum=%@", GW_Domain, self.goodId, self.ticketId, self.numberLabel.text] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        //        NSLog(@"-----%@", resultDict);
        NSDictionary *dic = resultDict[@"data"];
        _orderString = dic[@"order_sn"];
        } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
    
    
    //获取订单价格
    NSDictionary *dict = @{@"goods_id":self.goodId,
                           @"goods_sum":self.number};
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/order/getOrderPrice", GW_Domain] parameters:dict completionBlockWithSuccess:^(NSDictionary *resultDict) {
//        NSLog(@"%@", resultDict);
        NSDictionary *dic = resultDict[@"data"];
        _moneyLabel.text = [NSString stringWithFormat:@"%@", dic[@"price"]];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
    }];
    
}


- (void)initView
{
    UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, GWIDTH - 90, 49)];
    //    titleName.text = @"爱儿美儿童舞蹈秋季梦想儿童舞蹈";
    titleName.text = self.goodsName;
    titleName.textColor = ColorFrom16(0x7F7E7D);
    titleName.font = PFRFONT(14);
    [self.view addSubview:titleName];
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(GWIDTH - 17 - 100, 0, 100, 49)];
    priceLabel.text = @"580元";
    priceLabel.text = self.sale_price;
    priceLabel.textColor = ColorFrom16(0x7F7E7D);
    priceLabel.textAlignment = 2;
    priceLabel.font = PFRFONT(12);
    [self.view addSubview:priceLabel];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 49 , GWIDTH, 1)];
    line.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line];
    
    
    //数量
    UILabel *label = [UILabel new];
    label.text = @"数量";
    label.textColor = ColorFrom16(0x333333);
    label.font = PFRFONT(14);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(13);
        make.left.equalTo(self.view).offset(10);
        make.width.equalTo(@94);
        make.height.equalTo(@20);
    }];
    
    
    _numberLabel = [UILabel new];
    _numberLabel.text = @"1";
    _numberLabel.text = self.number;
    _numberLabel.textColor = ColorFrom16(0x333333);
//    _numberLabel.layer.borderColor = ColorFrom16(0xC2C2C2).CGColor;
//    _numberLabel.layer.borderWidth = 1;
    _numberLabel.textAlignment = 1;
    _numberLabel.font = PFRFONT(12);
    [self.view addSubview:_numberLabel];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(line.mas_bottom).offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@22);
    }];

    
    UIView *line1 = [UIView new];
    line1.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(13);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
    
    //优惠券
    UILabel *discountLabel = [UILabel new];
    discountLabel.text = @"优惠券";
    discountLabel.textColor = ColorFrom16(0x333333);
    discountLabel.font = PFRFONT(14);
    discountLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [discountLabel addGestureRecognizer:tap];
    [self.view addSubview:discountLabel];
    [discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(12);
        make.left.equalTo(self.view).offset(10);
        make.width.equalTo(@94);
        make.height.equalTo(@20);
    }];
    
    _reminderLabel = [UILabel new];
    _reminderLabel.text = @"暂无优惠券可用";
    _reminderLabel.textColor = ColorFrom16(0xFF5B7A);
    _reminderLabel.font = PFRFONT(12);
    _reminderLabel.userInteractionEnabled = YES;
    [_reminderLabel addGestureRecognizer:tap];
    [self.view addSubview:_reminderLabel];
    [_reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(discountLabel);
        make.right.equalTo(self.view).offset(-34);
        make.width.greaterThanOrEqualTo(@88);
        make.height.equalTo(@17);
    }];
    
    UIImageView *arrowImg = [UIImageView new];
    arrowImg.image = IMAGE(@"arrow-left");
    [self.view addSubview:arrowImg];
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(12);
        make.right.equalTo(self.view).offset(-8);
        make.width.equalTo(@16);
        make.height.equalTo(@16);
    }];
    
    UIView *line2 = [UIView new];
    line2.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(discountLabel.mas_bottom).offset(13);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
    
    //实付金额
    UILabel *paidLabel = [UILabel new];
    paidLabel.text = @"实付金额";
    paidLabel.textColor = ColorFrom16(0x333333);
    paidLabel.font = PFRFONT(14);
    [self.view addSubview:paidLabel];
    [paidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(13);
        make.left.equalTo(self.view).offset(10);
        make.width.equalTo(@73.8);
        make.height.equalTo(@20);
    }];
    
    _moneyLabel = [UILabel new];
    _moneyLabel.text = @"￥495";
    _moneyLabel.textColor = ColorFrom16(0xFF5B7A);
    _moneyLabel.font = PFRFONT(14);
    [self.view addSubview:_moneyLabel];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(13);
        make.right.equalTo(self.view).offset(-8);
        make.width.greaterThanOrEqualTo(@36.2);
        make.height.equalTo(@20);
    }];
    
    UIView *line3 = [UIView new];
    line3.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(paidLabel.mas_bottom).offset(13);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
    
    //提交订单按钮
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"提交订单" forState:0];
    [submitBtn setTitleColor:ColorFrom16(0x202020) forState:0];
    submitBtn.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    submitBtn.layer.cornerRadius = 7;
    submitBtn.titleLabel.font = PFRFONT(16);
    [submitBtn addTarget:self action:@selector(handleSubmitBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line3.mas_bottom).offset(29);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
}

- (void)tapAction
{
    UseDiscountViewController *VC = [UseDiscountViewController new];
    VC.goodId = self.goodId;
    VC.gsum = self.number;
    @weakobj(self);
    VC.block = ^(NSString *name, NSString *tiketId) {
        @strongobj(self);
        self.reminderLabel.text = name;
        self.ticketId = tiketId;
    };
    [self.navigationController pushViewController:VC animated:YES];
}

//提交订单响应按钮
- (void)handleSubmitBtn:(UIButton *)sender
{
    PayViewController *payVC = [PayViewController new];
    payVC.orderId = self.orderString;
    payVC.goodsName = self.goodsName;
    payVC.sale_price = self.moneyLabel.text;
    [UIView animateWithDuration:2.0 animations:^{
        [self.navigationController pushViewController:payVC animated:NO];
        
    }];
}

- (void)handleAddBtn:(UIButton *)sender
{
    _i += 1;
    _numberLabel.text = [NSString stringWithFormat:@"%d", _i];
    
    NSString *str = [NSString stringWithFormat:@"%d", (_i * [self.sale_price intValue])];
    _moneyLabel.text = str;
}

- (void)handleReduceBtn:(UIButton *)sender
{
    if(_i == 1)
    {
        _numberLabel.text = [NSString stringWithFormat:@"%d", 1];
        
        return;
    }
    else
    {
        _i -= 1;
        _numberLabel.text = [NSString stringWithFormat:@"%d", _i];
        
        NSString *str = [NSString stringWithFormat:@"%d", ( [self.moneyLabel.text intValue] / (_i + 1))];
        _moneyLabel.text = str;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
//    [self.navigationController.navigationBar setHidden:YES];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
//    [self.navigationController.navigationBar setHidden:NO];
    self.navigationController.navigationBar.backgroundColor = ColorClear;
}

@end
